<?php

namespace app\controller;

use app\BaseController;
use maotool\core\response\R;
use maotool\log\Console;
use maotool\office\xlsx\Sheet;
use maotool\office\xlsx\Xlsx;
use maotool\util\ArrayUtil;
use think\App;
use think\facade\Db;
use think\facade\Request;
use think\response\Json;

class Words extends BaseController
{

    public function index(): Json
    {
        return json(R::ok('success'));
    }

    public function detail(): Json
    {
        $index = Request::post('index', 0);
        $type = Request::post('type', 'current');
        $books = Db::table('books')->where('deleted_time', null)->find();
        $total = Db::table('words')->where('deleted_time', null)->count();
        if ($index == -1) {
            if ($books['index'] >= $total) {
                $books['index'] = 0;
            }
            if ($type == 'current') {
                $index = $books['index'];
            } else {
                $index = $books['index'] + 1;
            }
        }
        $words = Db::table('words')->where('deleted_time', null)
            ->limit($index, 1)
            ->order('sort', 'asc')
            ->order('id', 'asc')
            ->order('created_time', 'asc')
            ->select()
            ->toArray();
        $words = (array)$words[0];

        $words['annotate'] = json_decode($words['annotate'], true);
        $words['mark'] = json_decode($words['mark'], true);
        $nextIndex = $index + 1;
        if ($nextIndex >= $total) {
            $nextIndex = 0;
        }
        return json(R::ok([
            'index' => $index,
            'nextIndex' => $nextIndex,
            'words' => $words,
            'total' => $total,
        ]));
    }

    public function list(): Json
    {
        $list = Db::table('words')->where('deleted_time', null)
            ->select()
            ->toArray();
        foreach ($list as &$item) {
            $item = (array)$item;
            $item['annotate'] = json_decode($item['annotate'], true);
            $item['mark'] = json_decode($item['mark'], true);
        }
        return json(R::ok([
            'list' => $list,
        ]));
    }

    public function save(): Json
    {
        $id = Request::post('id');
        $index = Request::post('index', 0);
        $status = Request::post('status', 0);
        Db::table('words')->where('id', $id)
            ->where('deleted_time', null)->update([
                "status" => $status,
                "updated_time" => date('Y-m-d H:i:s'),
            ]);
        Db::table('books')->where('deleted_time', null)->update([
            "index" => $index,
            "updated_time" => date('Y-m-d H:i:s'),
        ]);
        return json(R::ok('success'));
    }

    public function export(): Json
    {
        return json(R::ok('success'));
    }

    public function import(): Json
    {
        set_time_limit(1000);
        $file = Request::file('file');
        if (empty($file)) {
            return json(R::err('请选择文件'));
        }
        $xlsx = new Xlsx();
        $sheet1 = new Sheet();
        $sheet1->setTitle("六级词汇表");
        $sheet1->setHeader([
            "sort" => "序号",
            "words" => "单词",
            "annotate" => "释义",
            "mark" => "音标",
        ]);
        $xlsx->setSheets([$sheet1]);
        $xlsx->readerStream($file);
        Db::table('words')->where('deleted_time', null)->delete();
        $all = $sheet1->getList();
        foreach ($all as &$item) {
            $item['annotate'] = json_encode($item['annotate'], JSON_UNESCAPED_UNICODE);
            $item['mark'] = json_encode($item['mark'], JSON_UNESCAPED_UNICODE);
            $item['created_time'] = date('Y-m-d H:i:s');
            $item['updated_time'] = date('Y-m-d H:i:s');
        }
        foreach (ArrayUtil::split($all, 500) as $list) {
            Db::table('words')->insertAll($list);
        }
        return json(R::ok($all));
    }

}